<?php


use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Biz\service\brand\BlindBox;
use Doraemon\service\position\Api;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\ImgUrl;
use Biz\service\NewProduct\NewProductService;

/**
 * 好物频道头部信息 - 福利社
 * Class ChannelHeadAction
 */
class WelfareAgencyAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const GOODS_BOTTOM_MAP = 'https://img.haohaozhu.cn/Op-imageShow/b81ad302w02w0Qv00r1bf5g?iv=1';
    const GOODS_TEST_NAME = '好好测';
    const NEW_PRODUCT_NAME = '新品速递';
    const FINE_GOODS_TEST = 'fine_goods_test'; //兼容线上历史数据 ，待上线完成后 可删除
    const NEW_PRODUCT = 'new_product';        //兼容线上历史数据 ，待上线完成后 可删除
    private $data = [];
    const ACTIVITY_YES = 1;
    const ACTIVITY_NO = 2;

    public function main()
    {
        $this->getBlindBox();
        $this->getWelfareAgencyBannerList();
        $this->getWelfareAgencyUserCount();
        $this->data = $this->data ? $this->data : (object)[];
        $this->setSuccess($this->data);
    }


    private function getBlindBox()
    {
        $result = [];
        $now = time();
        $where = [
            'activity_state' => mBlindBoxActivity::ACTIVITY_STATE_PASS,
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'prize_type' => [
                mBlindBoxActivity::PRIZE_TYPE_GOODS,
                mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON,
                mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON,
            ],
        ];
        $list = mBlindBoxActivity::selectByWhere($where, ['prize_title', 'prize_price', 'prize_img', 'prize_type']);
        foreach ($list as $key => $info) {
            if ($info['prize_type'] == mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON && empty($info['prize_img'])) {
                $list[$key]['prize_img'] = 'https://img.haohaozhu.cn/App-imageShow/o_phbig/ed7/34c2430dw0dw0D100ro58k6?h=1618&iv=1&w=1618&sw=f30';
            }
        }
        if ($list) {
            $imgs = array_filter(array_column($list, 'prize_img'));
            $result['prize_imgs'] = array_values($imgs);
            $result['list'] = $list;
            $result['link'] = BlindBox::getBlindBoxUrl();
            $result['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_FIXED_TOPIC_BOX, [],
                ['type' => 'blind_box']);
        }
        if (!empty($result)) {
            $this->data['blind_box_info'] = $result;
        }
    }

    private function getWelfareAgencyBannerList()
    {
        $list = array_slice(Api::tab4WelfareAgencyBanner($this->uid), 0, 2);
        $result = [];
        if ($list) {
            foreach ($list as $item) {
                $item['banner']['info'] = [];

                //兼容线上历史数据 ，待上线完成后 可删除
                if ($item['banner']['title'] == self::FINE_GOODS_TEST) {
                    $item['banner']['title'] = self::GOODS_TEST_NAME;
                }
                if ($item['banner']['title'] == self::NEW_PRODUCT) {
                    $item['banner']['title'] = self::NEW_PRODUCT_NAME;
                }
                $item['banner']['activity_type'] = self::ACTIVITY_YES;
                if (isset($item['banner']['goods_img']) && !empty($item['banner']['goods_img'])) {
                    $item ['banner']['info'] = ['goods_img' => $item['banner']['goods_img']];
                }

                if ($item['banner']['title'] == self::GOODS_TEST_NAME) {
                    $item ['banner']['info'] = $this->getGoodsTest();
                }
                if ($item['banner']['title'] == self::NEW_PRODUCT_NAME) {
                    $item ['banner']['info'] = $this->getNewProduct();
                }
                $result[] = $item;
            }
        }
        if (!empty($result)) {
            $this->data['welfare_agency_banner_list'] = $result;
        }
    }

    private function getGoodsTest(): array
    {
        $result = [];
        $now = time();
        $where = [
            'activity_state' => FineGoodsModel::STATE_PASS,
            'schedule_start_time[<=]' => $now,
            'schedule_end_time[>=]' => $now,
        ];
        $list = FineGoodsModel::all($where, ['cover_img']);
        $result['goods_img'] = self::GOODS_BOTTOM_MAP;
        if ($list) {
            $images = array_values(array_filter(array_column($list, 'cover_img_url')));
            $randKey = array_rand($images);
            $result['goods_img'] = $images[$randKey];
        }

        return $result;
    }

    private function getNewProduct(): array
    {
        $result = [];
        $list = NewProductService::getNewProductInfo([], 'img', 0, 99);
        $result['goods_img'] = self::GOODS_BOTTOM_MAP;
        $result['month'] = (int)date('m');
        if ($list) {
            $randKey = array_rand($list);
            $result['goods_img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $list[$randKey],
                ImgUrl::IMAGE_SIZE_ORI_NPHONE) ?? '';
        }

        return $result;
    }

    private function getWelfareAgencyUserCount()
    {
        //住友数 人数来源：取福利社（所有往期运营活动）累计uv总数，随页面的刷新展示
//        $uvSum = DailyReportWelfareClubUser::getData();

        $redis = new \Hhz\Redis\newCache(\hhz\constant\redis\redisKeys::KEY_B_CACHE_DAILY_REPORT_WELFARE_CLUB_USER);
        $uvSum =  $redis->getCache();
        $welfareAgencyUserCount = round(($uvSum / 10000), 1);
        $recommendationUser = clsWiki::getRandUserCoverImg(3);
        $this->data['welfare_agency_user']['avatars'] = $recommendationUser ? array_column($recommendationUser,
            'cover_img') : [];
        $this->data['welfare_agency_user']['count'] = $welfareAgencyUserCount . 'W住友参与';
    }


}